﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>进程函数列表 | AutoHotkey v2</title>
<meta name="description" content="This page lists functions for performing the following operations on a process: check if it exists; change its priority; close it; wait for it to exist; wait for it to close." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>进程函数</h1>

<p>用于对进程执行下列操作的函数: 检查它是否存在; 改变它的优先级; 关闭它; 等待它的存在; 等待它关闭. 单击函数名查看详细信息.</p>
<table class="info">
    <tr>
        <th>函数</th>
        <th abbr="Descr">描述</th>
    </tr>
    <tr>
        <td><a href="ProcessClose.htm">ProcessClose</a></td>
        <td>强制关闭第一个匹配的进程.</td>
    </tr>
    <tr>
        <td><a href="ProcessExist.htm">ProcessExist</a></td>
        <td>检查指定的进程是否存在.</td>
    </tr>
    <tr>
        <td><a href="ProcessGetName.htm">ProcessGetName</a></td>
        <td>返回指定进程的名称.</td>
    </tr>
    <tr>
        <td><a href="ProcessGetName.htm">ProcessGetParent</a></td>
        <td>返回创建指定进程的进程 ID(PID).</td>
    </tr>
    <tr>
        <td><a href="ProcessGetName.htm">ProcessGetPath</a></td>
        <td>返回指定进程的路径.</td>
    </tr>
    <tr>
        <td><a href="ProcessSetPriority.htm">ProcessSetPriority</a></td>
        <td>更改第一个匹配进程的优先级.</td>
    </tr>
    <tr>
        <td><a href="ProcessWait.htm">ProcessWait</a></td>
        <td>等待指定的进程存在.</td>
    </tr>
    <tr>
        <td><a href="ProcessWaitClose.htm">ProcessWaitClose</a></td>
        <td>等待匹配进程关闭.</td>
    </tr>
</table>

<h2 id="Remarks">备注</h2>
<p><strong>进程列表</strong>: 虽然没有 <em>ProcessList</em> 函数, 但<a href="#ExList">示例 #1</a> 和<a href="#ExListCOM">示例 #2</a> 演示了如何通过 <a href="DllCall.htm">DllCall</a> 或 COM 检索进程.</p>

<h2 id="Related">相关</h2>
<p><a href="Run.htm">Run</a>, <a href="WinClose.htm">WinClose</a>, <a href="WinKill.htm">WinKill</a>, <a href="WinWait.htm">WinWait</a>, <a href="WinWaitClose.htm">WinWaitClose</a>, <a href="WinExist.htm">WinExist</a>, <a href="Win.htm">窗口函数</a></p>

<h2 id="Examples">示例</h2>

<div class="ex" id="ExList">
<p><a class="ex_number" href="#ExList"></a> 通过 <a href="DllCall.htm">DllCall</a> 获取正在运行的进程列表然后显示在消息框中.</p>
<pre>d := "  |  "  <em>; 字符串分隔符</em>
s := 4096  <em>; 缓冲和数组的大小(4 KB)</em>

ScriptPID := ProcessExist()  <em>; 正在运行脚本的 PID.</em>
<em>; 使用 PROCESS_QUERY_INFORMATION(0x0400) 获取此脚本的句柄:</em>
h := DllCall("OpenProcess", "UInt", 0x0400, "Int", false, "UInt", ScriptPID, "Ptr")
<em>; 打开此进程的可调整的访问令牌(TOKEN_ADJUST_PRIVILEGES = 32):</em>
DllCall("Advapi32.dll\OpenProcessToken", "Ptr", h, "UInt", 32, "PtrP", &amp;t := 0)
<em>; 获取调试特权的本地唯一标识符:</em>
DllCall("Advapi32.dll\LookupPrivilegeValue", "Ptr", 0, "Str", "SeDebugPrivilege", "Int64P", &amp;luid := 0)
ti := Buffer(16, 0)  <em>; 特权结构</em>
NumPut( "UInt", 1  <em>; 特权数组中的一个条目...</em>
      , "Int64", luid
      , "UInt", 2  <em>; 启用这个特权: SE_PRIVILEGE_ENABLED = 2</em>
      , ti)
<em>; 使用新的访问令牌更新此进程的特权:</em>
r := DllCall("Advapi32.dll\AdjustTokenPrivileges", "Ptr", t, "Int", false, "Ptr", ti, "UInt", 0, "Ptr", 0, "Ptr", 0)
DllCall("CloseHandle", "Ptr", t)  <em>; 关闭此访问令牌句柄以节约内存.</em>
DllCall("CloseHandle", "Ptr", h)  <em>; 关闭此进程句柄以节约内存.</em>

hModule := DllCall("LoadLibrary", "Str", "Psapi.dll")  <em>; 通过预加载来提升性能.</em>
a := Buffer(s)  <em>; 接收进程列表标识符的数组:</em>
c := 0  <em>; 用于进程标识符的计数器</em>
l := ""
DllCall("Psapi.dll\EnumProcesses", "Ptr", a, "UInt", s, "UIntP", &amp;r)
Loop r // 4  <em>; 把数组解析为 DWORD(32 位) 的标识符:</em>
{
    id := NumGet(a, A_Index * 4, "UInt")
    <em>; 打开进程: PROCESS_VM_READ (0x0010) | PROCESS_QUERY_INFORMATION (0x0400)</em>
    h := DllCall("OpenProcess", "UInt", 0x0010 | 0x0400, "Int", false, "UInt", id, "Ptr")
    if !h
        continue
    n := Buffer(s, 0)  <em>; 接收模块基础名称的缓冲:</em>
    e := DllCall("Psapi.dll\GetModuleBaseName", "Ptr", h, "Ptr", 0, "Ptr", n, "UInt", s//2)
    if !e    <em>; 用于 64 位进程在 32 位模式时的回退方法:</em>
        e := DllCall("Psapi.dll\GetProcessImageFileName", "Ptr", h, "Ptr", n, "UInt", s//2)
    SplitPath StrGet(n), &amp;n
    DllCall("CloseHandle", "Ptr", h)  <em>; 关闭进程句柄以节约内存</em>
    if (n &amp;&amp; e)  <em>; 如果映像不是空的, 则添加到列表:</em>
        l .= n "`n", c++
}
DllCall("FreeLibrary", "Ptr", hModule)  <em>; 卸载库来释放内存.</em>
<em>;l := Sort(l)  ; 取消注释这行来按字母顺序对列表进行排序.</em>
MsgBox StrReplace(l, "`n", d), c " Processes", 0</pre>
</div>

<div class="ex" id="ExListCOM">
<p><a class="ex_number" href="#ExListCOM"></a> 使用 COM 获取正在运行的进程列表.</p>
<pre>MyGui := Gui(, "Process List")
LV := MyGui.Add("ListView", "x2 y0 w400 h500", ["Process Name","Command Line"])
for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process")
    LV.Add("", process.Name, process.CommandLine)
MyGui.Show

<em>; Win32_Process: <a href="http://msdn.microsoft.com/en-us/library/aa394372.aspx">http://msdn.microsoft.com/en-us/library/aa394372.aspx</a></em>
</pre>
</div>

</body>
</html>